﻿<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:Controls="clr-namespace:MahApps.Metro.Controls"
                    xmlns:Converters="clr-namespace:MahApps.Metro.Converters">

    <Converters:ToUpperConverter x:Key="ToUpperConverter" />
    <Converters:ThicknessBindingConverter x:Key="ThicknessBindingConverter" />

    <Style x:Key="ExpanderBaseHeaderStyle"
           TargetType="{x:Type ToggleButton}">
        <Setter Property="Background"
                Value="Transparent" />
        <Setter Property="BorderBrush"
                Value="Transparent" />
        <Setter Property="BorderThickness"
                Value="0" />
        <Setter Property="Margin"
                Value="4" />
        <Setter Property="Padding"
                Value="0" />
        <Setter Property="FocusVisualStyle"
                Value="{x:Null}" />
        <Setter Property="SnapsToDevicePixels"
                Value="True" />
    </Style>

    <Style x:Key="ExpanderRightHeaderStyle"
           TargetType="{x:Type ToggleButton}"
           BasedOn="{StaticResource ExpanderBaseHeaderStyle}">
        <Setter Property="HorizontalContentAlignment"
                Value="Center" />
        <Setter Property="VerticalContentAlignment"
                Value="Stretch" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ToggleButton}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                            Padding="{TemplateBinding Padding}">
                        <Grid Background="Transparent"
                              SnapsToDevicePixels="False">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="19" />
                                <RowDefinition Height="*" />
                            </Grid.RowDefinitions>
                            <Grid>
                                <Grid.LayoutTransform>
                                    <TransformGroup>
                                        <TransformGroup.Children>
                                            <TransformCollection>
                                                <RotateTransform Angle="-90" />
                                            </TransformCollection>
                                        </TransformGroup.Children>
                                    </TransformGroup>
                                </Grid.LayoutTransform>
                                <Ellipse x:Name="Circle"
                                         Width="19"
                                         Height="19"
                                         HorizontalAlignment="Center"
                                         VerticalAlignment="Center"
                                         Stroke="{TemplateBinding Foreground}" />
                                <Path x:Name="Arrow"
                                      HorizontalAlignment="Center"
                                      VerticalAlignment="Center"
                                      Data="M 1,1.5 L 4.5,5 L 8,1.5"
                                      SnapsToDevicePixels="false"
                                      Stroke="{TemplateBinding Foreground}"
                                      StrokeThickness="2" />
                            </Grid>
                            <ContentPresenter Grid.Row="1"
                                              Margin="0,4,0,0"
                                              HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                              RecognizesAccessKey="True"
                                              SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                        </Grid>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsChecked"
                                 Value="true">
                            <Setter TargetName="Arrow"
                                    Property="Data"
                                    Value="M 1,4.5  L 4.5,1  L 8,4.5" />
                        </Trigger>
                        <Trigger Property="IsMouseOver"
                                 Value="true">
                            <Setter TargetName="Circle"
                                    Property="Stroke"
                                    Value="{DynamicResource GrayBrush2}" />
                            <Setter TargetName="Arrow"
                                    Property="Stroke"
                                    Value="{DynamicResource GrayBrush2}" />
                        </Trigger>
                        <Trigger Property="IsPressed"
                                 Value="true">
                            <Setter TargetName="Circle"
                                    Property="Stroke"
                                    Value="{DynamicResource BlackColorBrush}" />
                            <Setter TargetName="Circle"
                                    Property="StrokeThickness"
                                    Value="2" />
                            <Setter TargetName="Arrow"
                                    Property="Stroke"
                                    Value="{DynamicResource BlackColorBrush}" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="ExpanderUpHeaderStyle"
           TargetType="{x:Type ToggleButton}"
           BasedOn="{StaticResource ExpanderBaseHeaderStyle}">
        <Setter Property="HorizontalContentAlignment"
                Value="Stretch" />
        <Setter Property="VerticalContentAlignment"
                Value="Center" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ToggleButton}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            Padding="{TemplateBinding Padding}">
                        <Grid Background="Transparent"
                              SnapsToDevicePixels="False">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="19" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <Grid>
                                <Grid.LayoutTransform>
                                    <TransformGroup>
                                        <TransformGroup.Children>
                                            <TransformCollection>
                                                <RotateTransform Angle="180" />
                                            </TransformCollection>
                                        </TransformGroup.Children>
                                    </TransformGroup>
                                </Grid.LayoutTransform>
                                <Ellipse x:Name="Circle"
                                         Width="19"
                                         Height="19"
                                         HorizontalAlignment="Center"
                                         VerticalAlignment="Center"
                                         Stroke="{TemplateBinding Foreground}" />
                                <Path x:Name="Arrow"
                                      HorizontalAlignment="Center"
                                      VerticalAlignment="Center"
                                      Data="M 1,1.5 L 4.5,5 L 8,1.5"
                                      SnapsToDevicePixels="false"
                                      Stroke="{TemplateBinding Foreground}"
                                      StrokeThickness="2" />
                            </Grid>
                            <ContentPresenter Grid.Column="1"
                                              Margin="4,0,0,0"
                                              HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                              RecognizesAccessKey="True"
                                              SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                        </Grid>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsChecked"
                                 Value="true">
                            <Setter TargetName="Arrow"
                                    Property="Data"
                                    Value="M 1,4.5  L 4.5,1  L 8,4.5" />
                        </Trigger>
                        <Trigger Property="IsMouseOver"
                                 Value="true">
                            <Setter TargetName="Circle"
                                    Property="Stroke"
                                    Value="{DynamicResource GrayBrush2}" />
                            <Setter TargetName="Arrow"
                                    Property="Stroke"
                                    Value="{DynamicResource GrayBrush2}" />
                        </Trigger>
                        <Trigger Property="IsPressed"
                                 Value="true">
                            <Setter TargetName="Circle"
                                    Property="Stroke"
                                    Value="{DynamicResource BlackColorBrush}" />
                            <Setter TargetName="Circle"
                                    Property="StrokeThickness"
                                    Value="2" />
                            <Setter TargetName="Arrow"
                                    Property="Stroke"
                                    Value="{DynamicResource BlackColorBrush}" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="ExpanderLeftHeaderStyle"
           TargetType="{x:Type ToggleButton}"
           BasedOn="{StaticResource ExpanderRightHeaderStyle}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ToggleButton}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            Padding="{TemplateBinding Padding}">
                        <Grid Background="Transparent"
                              SnapsToDevicePixels="False">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="19" />
                                <RowDefinition Height="*" />
                            </Grid.RowDefinitions>
                            <Grid>
                                <Grid.LayoutTransform>
                                    <TransformGroup>
                                        <TransformGroup.Children>
                                            <TransformCollection>
                                                <RotateTransform Angle="90" />
                                            </TransformCollection>
                                        </TransformGroup.Children>
                                    </TransformGroup>
                                </Grid.LayoutTransform>
                                <Ellipse x:Name="Circle"
                                         Width="19"
                                         Height="19"
                                         HorizontalAlignment="Center"
                                         VerticalAlignment="Center"
                                         Stroke="{TemplateBinding Foreground}" />
                                <Path x:Name="Arrow"
                                      HorizontalAlignment="Center"
                                      VerticalAlignment="Center"
                                      Data="M 1,1.5 L 4.5,5 L 8,1.5"
                                      SnapsToDevicePixels="false"
                                      Stroke="{TemplateBinding Foreground}"
                                      StrokeThickness="2" />
                            </Grid>
                            <ContentPresenter Grid.Row="1"
                                              Margin="0,4,0,0"
                                              HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                              RecognizesAccessKey="True"
                                              SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                        </Grid>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsChecked"
                                 Value="true">
                            <Setter TargetName="Arrow"
                                    Property="Data"
                                    Value="M 1,4.5  L 4.5,1  L 8,4.5" />
                        </Trigger>
                        <Trigger Property="IsMouseOver"
                                 Value="true">
                            <Setter TargetName="Circle"
                                    Property="Stroke"
                                    Value="{DynamicResource GrayBrush2}" />
                            <Setter TargetName="Arrow"
                                    Property="Stroke"
                                    Value="{DynamicResource GrayBrush2}" />
                        </Trigger>
                        <Trigger Property="IsPressed"
                                 Value="true">
                            <Setter TargetName="Circle"
                                    Property="Stroke"
                                    Value="{DynamicResource BlackColorBrush}" />
                            <Setter TargetName="Circle"
                                    Property="StrokeThickness"
                                    Value="2" />
                            <Setter TargetName="Arrow"
                                    Property="Stroke"
                                    Value="{DynamicResource BlackColorBrush}" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="ExpanderDownHeaderStyle"
           TargetType="{x:Type ToggleButton}"
           BasedOn="{StaticResource ExpanderUpHeaderStyle}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ToggleButton}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            Padding="{TemplateBinding Padding}">
                        <Grid Background="Transparent"
                              SnapsToDevicePixels="False">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="19" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <Ellipse x:Name="Circle"
                                     Width="19"
                                     Height="19"
                                     HorizontalAlignment="Center"
                                     VerticalAlignment="Center"
                                     Stroke="{TemplateBinding Foreground}" />
                            <Path x:Name="Arrow"
                                  HorizontalAlignment="Center"
                                  VerticalAlignment="Center"
                                  Data="M 1,1.5 L 4.5,5 L 8,1.5"
                                  SnapsToDevicePixels="false"
                                  Stroke="{TemplateBinding Foreground}"
                                  StrokeThickness="2" />
                            <ContentPresenter Grid.Column="1"
                                              Margin="4,0,0,0"
                                              HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                              RecognizesAccessKey="True"
                                              SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                        </Grid>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsChecked"
                                 Value="true">
                            <Setter TargetName="Arrow"
                                    Property="Data"
                                    Value="M 1,4.5  L 4.5,1  L 8,4.5" />
                        </Trigger>
                        <Trigger Property="IsMouseOver"
                                 Value="true">
                            <Setter TargetName="Circle"
                                    Property="Stroke"
                                    Value="{DynamicResource GrayBrush2}" />
                            <Setter TargetName="Arrow"
                                    Property="Stroke"
                                    Value="{DynamicResource GrayBrush2}" />
                        </Trigger>
                        <Trigger Property="IsPressed"
                                 Value="true">
                            <Setter TargetName="Circle"
                                    Property="Stroke"
                                    Value="{DynamicResource BlackColorBrush}" />
                            <Setter TargetName="Circle"
                                    Property="StrokeThickness"
                                    Value="2" />
                            <Setter TargetName="Arrow"
                                    Property="Stroke"
                                    Value="{DynamicResource BlackColorBrush}" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="MetroExpander"
           TargetType="{x:Type Expander}">
        <Setter Property="Padding"
                Value="5" />
        <Setter Property="SnapsToDevicePixels"
                Value="True" />
        <Setter Property="Foreground"
                Value="{DynamicResource BlackBrush}" />
        <Setter Property="Background"
                Value="{DynamicResource AccentColorBrush}" />
        <Setter Property="BorderBrush"
                Value="{DynamicResource AccentColorBrush}" />
        <Setter Property="BorderThickness"
                Value="1" />
        <Setter Property="Controls:ControlsHelper.HeaderFontSize"
                Value="{DynamicResource ContentFontSize}" />
        <Setter Property="Controls:GroupBoxHelper.HeaderForeground"
                Value="{x:Null}" />
        <Setter Property="Controls:ExpanderHelper.HeaderUpStyle"
                Value="{StaticResource ExpanderUpHeaderStyle}" />
        <Setter Property="Controls:ExpanderHelper.HeaderDownStyle"
                Value="{StaticResource ExpanderDownHeaderStyle}" />
        <Setter Property="Controls:ExpanderHelper.HeaderLeftStyle"
                Value="{StaticResource ExpanderLeftHeaderStyle}" />
        <Setter Property="Controls:ExpanderHelper.HeaderRightStyle"
                Value="{StaticResource ExpanderRightHeaderStyle}" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Expander}">
                    <Grid>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal" />
                                <VisualState x:Name="MouseOver" />
                                <VisualState x:Name="Disabled">
                                    <Storyboard>
                                        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity"
                                                                       Storyboard.TargetName="DisabledVisualElement">
                                            <SplineDoubleKeyFrame KeyTime="0"
                                                                  Value="0.7" />
                                        </DoubleAnimationUsingKeyFrames>
                                        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)"
                                                                       Storyboard.TargetName="ExpanderRoot">
                                            <EasingDoubleKeyFrame KeyTime="0"
                                                                  Value="0.3" />
                                        </DoubleAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Rectangle x:Name="DisabledVisualElement"
                                   Fill="{DynamicResource ControlsDisabledBrush}"
                                   IsHitTestVisible="false"
                                   Opacity="0" />
                        <DockPanel x:Name="ExpanderRoot">
                            <Border x:Name="HeaderSite"
                                    Background="{TemplateBinding Background}"
                                    BorderBrush="{TemplateBinding BorderBrush}"
                                    SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                    UseLayoutRounding="True"
                                    BorderThickness="{TemplateBinding BorderThickness}"
                                    DockPanel.Dock="Top">
                                <ToggleButton x:Name="ToggleSite"
                                              UseLayoutRounding="False"
                                              IsChecked="{Binding IsExpanded, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
                                              Content="{TemplateBinding Header}"
                                              ContentTemplate="{TemplateBinding HeaderTemplate}"
                                              ContentTemplateSelector="{TemplateBinding HeaderTemplateSelector}"
                                              ContentStringFormat="{TemplateBinding HeaderStringFormat}"
                                              FontSize="{TemplateBinding Controls:ControlsHelper.HeaderFontSize}"
                                              FontStretch="{TemplateBinding Controls:ControlsHelper.HeaderFontStretch}"
                                              FontWeight="{TemplateBinding Controls:ControlsHelper.HeaderFontWeight}"
                                              Style="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(Controls:ExpanderHelper.HeaderDownStyle)}">
                                    <ToggleButton.Foreground>
                                        <MultiBinding Converter="{x:Static Converters:BackgroundToForegroundConverter.Instance}">
                                            <Binding Mode="OneWay"
                                                     Path="Background"
                                                     RelativeSource="{RelativeSource TemplatedParent}" />
                                            <Binding Mode="OneWay"
                                                     Path="(Controls:GroupBoxHelper.HeaderForeground)"
                                                     RelativeSource="{RelativeSource TemplatedParent}" />
                                        </MultiBinding>
                                    </ToggleButton.Foreground>
                                </ToggleButton>
                            </Border>
                            <Border x:Name="ExpandSite"
                                    HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                    VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                    SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                    UseLayoutRounding="True"
                                    Background="Transparent"
                                    BorderBrush="{TemplateBinding BorderBrush}"
                                    BorderThickness="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=BorderThickness, Converter={StaticResource ThicknessBindingConverter}, ConverterParameter={x:Static Converters:IgnoreThicknessSideType.Top}}"
                                    DockPanel.Dock="Bottom"
                                    Focusable="false"
                                    Opacity="0"
                                    Visibility="Collapsed">
                                <ContentPresenter UseLayoutRounding="False"
                                                  Margin="{TemplateBinding Padding}"
                                                  Content="{TemplateBinding Content}"
                                                  ContentTemplate="{TemplateBinding ContentTemplate}"
                                                  Cursor="{TemplateBinding Cursor}" />
                            </Border>
                        </DockPanel>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="Controls:ControlsHelper.PreserveTextCase"
                                 Value="False">
                            <Setter TargetName="ToggleSite"
                                    Property="Content"
                                    Value="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=Header, Mode=OneWay, Converter={StaticResource ToUpperConverter}}" />
                        </Trigger>
                        <Trigger Property="IsExpanded"
                                 Value="true">
                            <Setter TargetName="ExpandSite"
                                    Property="Visibility"
                                    Value="Visible" />
                            <Trigger.EnterActions>
                                <BeginStoryboard>
                                    <Storyboard>
                                        <DoubleAnimation Duration="0:0:0.25"
                                                         Storyboard.TargetName="ExpandSite"
                                                         Storyboard.TargetProperty="(UIElement.Opacity)"
                                                         To="1" />
                                    </Storyboard>
                                </BeginStoryboard>
                            </Trigger.EnterActions>
                            <Trigger.ExitActions>
                                <BeginStoryboard>
                                    <Storyboard>
                                        <DoubleAnimation Duration="0:0:0.25"
                                                         Storyboard.TargetName="ExpandSite"
                                                         Storyboard.TargetProperty="(UIElement.Opacity)"
                                                         To="0" />
                                    </Storyboard>
                                </BeginStoryboard>
                            </Trigger.ExitActions>
                        </Trigger>
                        <Trigger Property="ExpandDirection"
                                 Value="Right">
                            <Setter TargetName="ExpandSite"
                                    Property="DockPanel.Dock"
                                    Value="Right" />
                            <Setter TargetName="ExpandSite"
                                    Property="BorderThickness"
                                    Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=BorderThickness, Converter={StaticResource ThicknessBindingConverter}, ConverterParameter={x:Static Converters:IgnoreThicknessSideType.Left}}" />
                            <Setter TargetName="HeaderSite"
                                    Property="DockPanel.Dock"
                                    Value="Left" />
                            <Setter TargetName="ToggleSite"
                                    Property="Style"
                                    Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(Controls:ExpanderHelper.HeaderRightStyle)}" />
                        </Trigger>
                        <Trigger Property="ExpandDirection"
                                 Value="Up">
                            <Setter TargetName="ExpandSite"
                                    Property="DockPanel.Dock"
                                    Value="Top" />
                            <Setter TargetName="ExpandSite"
                                    Property="BorderThickness"
                                    Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=BorderThickness, Converter={StaticResource ThicknessBindingConverter}, ConverterParameter={x:Static Converters:IgnoreThicknessSideType.Bottom}}" />
                            <Setter TargetName="HeaderSite"
                                    Property="DockPanel.Dock"
                                    Value="Bottom" />
                            <Setter TargetName="ToggleSite"
                                    Property="Style"
                                    Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(Controls:ExpanderHelper.HeaderUpStyle)}" />
                        </Trigger>
                        <Trigger Property="ExpandDirection"
                                 Value="Left">
                            <Setter TargetName="ExpandSite"
                                    Property="DockPanel.Dock"
                                    Value="Left" />
                            <Setter TargetName="ExpandSite"
                                    Property="BorderThickness"
                                    Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=BorderThickness, Converter={StaticResource ThicknessBindingConverter}, ConverterParameter={x:Static Converters:IgnoreThicknessSideType.Right}}" />
                            <Setter TargetName="HeaderSite"
                                    Property="DockPanel.Dock"
                                    Value="Right" />
                            <Setter TargetName="ToggleSite"
                                    Property="Style"
                                    Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(Controls:ExpanderHelper.HeaderLeftStyle)}" />
                        </Trigger>
                        <Trigger Property="IsEnabled"
                                 Value="false">
                            <Setter Property="Foreground"
                                    Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>
